package org.zxxxt.shortlink.admin.controller;


import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.zxxxt.shortlink.admin.common.convention.result.Result;
import org.zxxxt.shortlink.admin.common.convention.result.Results;
import org.zxxxt.shortlink.admin.common.enums.UserErrorCodeEnum;
import org.zxxxt.shortlink.admin.dto.req.UserLoginReqDTO;
import org.zxxxt.shortlink.admin.dto.req.UserRegisterReqDTO;
import org.zxxxt.shortlink.admin.dto.req.UserUpdateReqDTO;
import org.zxxxt.shortlink.admin.dto.resp.UserLoginRespDTO;
import org.zxxxt.shortlink.admin.dto.resp.UserRespDTO;
import org.zxxxt.shortlink.admin.service.UserService;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询用户信息
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> hello(@PathVariable("username")String username ) {
        UserRespDTO result = userService.getUserByUsername(username);
        if (result == null) {
            return new Result<UserRespDTO>().setCode(UserErrorCodeEnum.USER_NULL.code()).setMessage(UserErrorCodeEnum.USER_NULL.message());
        } else {
            return new Result<UserRespDTO>().setCode("0").setData(result);
        }
    }

    /**
     *  查询用户名是否存在
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hashUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     *  注册用户
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    /**
     * 修改用户
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }


    /**
     * 用户登录
     */

    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        UserLoginRespDTO result = userService.login(requestParam);
        return Results.success(result);
    }


    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token")String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 退出登录
     */
    @GetMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }
}
